//
//  OrigPubInputTextCell.m
//  ShrimpCrowdordering
//
//  Created by jimmy on 2018/8/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "OrigPubInputTextCell.h"

@interface OrigPubInputTextCell ()<UITextFieldDelegate>
{
    CGFloat _cellHeight;
    BOOL    _isAdaptive;
}
@property (nonatomic, strong) UILabel           * titleLab;
@property (nonatomic, strong) UITextField       * textTF;
@property (nonatomic, strong) UILabel           * untilLab;

@end


@implementation OrigPubInputTextCell



+ (id)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height  isAdaptive:(BOOL)isAdaptive keyBoardType:(UIKeyboardType )keyBoardType
{
    static NSString *cellID = @"OrigPubInputTextCell";
    OrigPubInputTextCell *cell = [[OrigPubInputTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID andIndexPath:indexPath andCellHeight:height isAdaptive:isAdaptive keyBoardType:keyBoardType];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textTF.keyboardType = keyBoardType;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height  isAdaptive:(BOOL)isAdaptive keyBoardType:(UIKeyboardType )keyBoardType
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _cellHeight = height;
        _isAdaptive = isAdaptive;
        [self setUIViewKeyBoardType:keyBoardType];
    }
    
    return self;
}

- (void)setUIViewKeyBoardType:(UIKeyboardType )keyBoardType
{
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.font = [UIFont boldSystemFontOfSize:16];
    self.titleLab.textColor = [UIColor colorWithHexString:@"#212121"];
    [self addSubview:self.titleLab];
    
    self.textTF = [[UITextField alloc] init];
    self.textTF.delegate = self;
    self.textTF.keyboardType = keyBoardType;
    self.textTF.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.textTF];
    
    self.untilLab = [[UILabel alloc] init];
    self.untilLab.font = [UIFont systemFontOfSize:12];
    self.untilLab.textColor = [UIColor colorWithHexString:@"#ababab"];
    [self addSubview:self.untilLab];
    
    UILabel * noteLab = [[UILabel alloc] init];
    noteLab.font = [UIFont systemFontOfSize:12];
    noteLab.text = @"请填写";
    noteLab.textColor = [UIColor colorWithHexString:@"#f21934"];
    [self addSubview:noteLab];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self addSubview:lineView];
    
    self.titleLab.frame = CGRectMake(ScreenX375(16), (_cellHeight - ScreenX375(20))/2, ScreenX375(80), ScreenX375(20));
    
    if (_isAdaptive) {
        self.untilLab.hidden = YES;
        
        noteLab.frame = CGRectMake(SCREEN_WIDTH - ScreenX375(40) - ScreenX375(13), (_cellHeight - ScreenX375(20))/2, ScreenX375(60), ScreenX375(20));
        
        self.textTF.frame = CGRectMake(ScreenX375(16) + ScreenX375(80), (_cellHeight - ScreenX375(20))/2, SCREEN_WIDTH - ScreenX375(40) - ScreenX375(80) - ScreenX375(16) - ScreenX375(13), ScreenX375(20));
        lineView.frame = CGRectMake(ScreenX375(16) + ScreenX375(80), (_cellHeight - ScreenX375(20))/2 + ScreenX375(22), SCREEN_WIDTH - ScreenX375(80) - ScreenX375(13) - ScreenX375(16), 1);
    }else{
        self.untilLab.hidden = NO;
        self.textTF.frame = CGRectMake(ScreenX375(16) + ScreenX375(80), (_cellHeight - ScreenX375(20))/2, ScreenX375(80), ScreenX375(20));
        lineView.frame = CGRectMake(ScreenX375(16) + ScreenX375(80), (_cellHeight - ScreenX375(20))/2 + ScreenX375(22), ScreenX375(80), 1);
        self.untilLab.frame = CGRectMake(ScreenX375(16) + ScreenX375(80)*2, (_cellHeight - 20)/2, ScreenX375(25), ScreenX375(25));
        noteLab.frame = CGRectMake(ScreenX375(16) + ScreenX375(80)*2+ScreenX375(25), (_cellHeight - ScreenX375(20))/2, ScreenX375(60), ScreenX375(20));
    }
}

- (void)setInputText:(NSString *)text
{
    self.textTF.text = text;
}

- (void)setUIWithTitle:(NSString *)title placeholder:(NSString *)placeholder until:(NSString *)until
{
    self.titleLab.text = title;
    self.textTF.placeholder = placeholder;
    
    if (until == nil) {
        self.untilLab.hidden = YES;
    }else{
        self.untilLab.hidden = NO;
        self.untilLab.text = until;
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (self.inputChangeBlock) {
        self.inputChangeBlock(self.textTF.text);
    }
}


@end
